public class SearchMatrix {
    public static void main(String[] args) {
        SearchMatrix searchMatrix = new SearchMatrix();
        int[][] nums = {
                {1,   3,  5,  7},
                {10, 11, 16, 20},
                {23, 30, 34, 50}
        };
        boolean b = searchMatrix.searchMatrix(nums, 1);

        int[] ints = {3,1,2};
        System.out.println(searchMatrix.findMin(ints));
        System.out.println(b);
    }
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length==0)return false;
        int m = matrix.length;
        int n = matrix[0].length;
        int left =0;
        int right = m*n -1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(matrix[mid/n][mid%n]==target){
                return true;
            }else if(matrix[mid/n][mid%n] > target){
                right = mid-1;
            }else{
                left = mid +1;
            }
        }
        return false;
    }
    public int findMin(int[] nums) {
        // 寻找断层点
        int left=0;
        int right = nums.length-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if((mid+1)<=nums.length-1 && nums[mid+1]<nums[mid])return nums[mid+1];
            if((mid-1)>=0 && nums[mid-1]>nums[mid])return nums[mid];
            if(nums[mid]>nums[left]){
                left = mid+1;
            }else{
                right = mid -1;
            }
        }
        return nums[0];
    }
}
